package com.xdap.wecom.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.common.collect.Maps;
import com.xdap.common.config.LocalValue;
import com.xdap.common.consts.ComConst;
import com.xdap.common.util.ComPluginPropertiesUtil;
import com.xdap.wecom.pojo.dto.WeComMsgDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @author: jianfeng.zheng
 * @since: 2022/6/21 2:22 下午
 * @history: 1.2022/6/21 created by jianfeng.zheng
 */
@Slf4j
@Service
@LocalValue
public class WeComClient {
    @LocalValue("${techking.wecom.corpid}")
    private String appKey;
    @LocalValue("${techking.wecom.corpsecret}")
    private String appSecret;
    @LocalValue("${techking.wecom.agentId}")
    private String agentId;
    @LocalValue("${techking.wecom.domain}")
    private String domain;
    @Resource
    private HttpRequestClient httpClient;
    //  private WeComConfig config;

    //token过期时间，7200秒
    private static String token;
    private static final Long MAX_TOKEN_TIMEOUT = 1000 * 7200L;
    private Long lastTokenTime = 0L;


    /**
     * 刷新token
     *
     * @return
     */
    public String refreshToken() {
        Long currentTime = System.currentTimeMillis();
        if (StringUtils.isBlank(token) || currentTime - lastTokenTime > MAX_TOKEN_TIMEOUT) {
            String property = ComPluginPropertiesUtil.getProperty("techking.wecom.api.getToken");
            String domain = ComPluginPropertiesUtil.getProperty("techking.wecom.domain");
            String url = domain + property;
            Map<String, String> requestParams = Maps.newHashMap();
            requestParams.put("corpid", appKey);
            requestParams.put("corpsecret", appSecret);
            log.info("获取企业微信token");
            String json = httpClient.get(url, requestParams);
            WeComResponse response = JSON.parseObject(json, new TypeReference<WeComResponse>() {
            });
            if (response.getErrcode() == 0) {
                token = response.getAccess_token();
                lastTokenTime = currentTime;
            }
        }
        return token;
    }

    /**
     * @Description 发送微信文本卡片消息
     * @Date 2023/11/21 16:24
     * @Author shangsheng.li
     **/
    public WeComResponse sendWxTextMsg(WeComMsgDto weComMsgDto) {
        Map<String, String> params = new HashMap<>();
        String token = this.refreshToken();
        String domain = ComPluginPropertiesUtil.getProperty("techking.wecom.domain");
        params.put(ComConst.ACCESS_TOKEN, token);
        String property = ComPluginPropertiesUtil.getProperty("techking.wecom.api.msgSend");
        String json = httpClient.post(domain + property, weComMsgDto, params);
        WeComResponse response = JSON.parseObject(json, new TypeReference<WeComResponse>() {
        });
        return response;
    }


}
